<?php
/*define('IN_ADMINCP', TRUE);
define('IN_TESTCP', TRUE);
define('TESTCP_AJAX', AJAX);
define('TESTCP_PATH', dirname(__FILE__));
define('APP_PATH', dirname(TESTCP_PATH));
//echo TESTCP_PATH . '--' . APP_PATH;die;
require APP_PATH . '/app.php';
$admincp=new admincp;
$account = new admincp_account;*/
//上面的代码采用杨坤的框架，下面的代码采用原生PHP
include "center/Model.class.php";
include "center/Route.class.php";
class Update extends Model{
    public $admincp='';
    public $error='';
    public $data='';
    public $level1Data='';
    public $level2Data='';
    public $level3Data='';
    public $admin='小编';
    public function __construct(){
    }
    public function Action(){
        //提交更改
        if(isset($_POST['submit'])){
            $this->Update();
        }
        //数据
        return $this->Show();
    }
    private function Show(){
        //获取第一级的分类
        $this->level1Data=$this->levelData(1);
        $id=$_GET['updateid'];
        $sql="SELECT * FROM oa_download WHERE id=$id";
        $this->data=static::one($sql);
        $this->level2Data=$this->levelData(2,$this->data['level1']);
        $this->level3Data=$this->levelData(3,$this->data['level2']);
        return;
    }
    private function levelData($level,$cid=null){
        if($cid!=null){
            $sql="SELECT * FROM oa_download_category WHERE level=$level AND parent_cid=$cid";
        }else{
            $sql="SELECT * FROM oa_download_category WHERE level=$level";
        }
        return parent::all($sql);
    }
    private function Update(){
        $data=array(
            'admin'=>"'".$this->admin."'",
            'name'=>"'".$_POST['name']."'",
            'url'=>"'".$_POST['url']."'",
            'cids'=>"'".$_POST['level1'].'/'.$_POST['level2'].'/'.$_POST['level3']."'",
            'password'=>"'".$_POST['password']."'",
            'time'=>time(),
            'level1'=>"'".$_POST['level1']."'",
            'level2'=>"'".$_POST['level2']."'",
            'level3'=>"'".$_POST['level3']."'",
            'id'=>$_POST['id'],
        );
        if(empty($_POST['name']) || empty($_POST['url'])){
            return $this->error='必填选项不得为空！';
        }else{
            $sql="UPDATE oa_download
                         SET admin={$data['admin']},
                             name={$data['name']},
                             url={$data['url']},
                             level1={$data['level1']},
                             level2={$data['level2']},
                             level3={$data['level3']},
                             password={$data['password']}
                       WHERE id={$data['id']}
                         ";
            $a=parent::aud($sql);
            if($a==1){
                echo '<script>alert("文档修改成功！");</script>';
            }else{
                echo '<script>alert("文档修改失败！请检查文档！");</script>';
            }
        }
    }
}
$model=new Update();
$model->Action();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>后台管理</title>
    <link rel="stylesheet" type="text/css" href="assets/css/editor.css"/>
    <link rel="stylesheet" type="text/css" href="assets/css/public.css"/>
    <script type="text/javascript" src="assets/js/jquery-1.9.1.min.js"></script>
</head>
<body>
<div id="content">
    <div id="content-box">
        <div class="content-head">
            <div class="keyboard padding-l40">
                <input type="text" class="search padding-l40 color-black" placeholder="站内搜索"/>
                <div class="keyboard-botton">
                    <a href="javascript:void(0);"><img src="assets/backend-img/content-head/settings.png"/></a>
                    <a href="javascript:void(0);"><img src="assets/backend-img/content-head/language.png"/></a>
                    <a href="javascript:void(0);" class="information"><img src="assets/backend-img/content-head/information.png"/></a>
                    <a href="javascript:void(0);" class="on-off"><img src="assets/backend-img/content-head/on-off.png"/></a>
                </div>
            </div>
            <div class="navigation padding-l40">
                <a href="javascript:void(0);">导航管理</a><a href="javascript:void(0);" class="rating">文档列表</a><span>修改后记得保存！</span>
            </div>
        </div>
        <div class="content">
            <div class="box">
                <form action="" method="post">
                    <div style="color:red;">
                        <input type="hidden" name='id' value="<?= $model->data['id']; ?>"/>
                        <?php if(isset($addModel->error)){ echo $addModel->error;}?>
                    </div>
                    <div class="input width60">
                        <label>文档标题：</label>
                        <input type="text" name="name" class="input-text400" placeholder="请输入文章标题" value="<?= $model->data['name'];?>"/>
                        <span style="color:red;">*(必填)</span>
                    </div>
                    <div class="state">
                        <div class="float-l">
                            业务方向：
                        </div>
                        <?php foreach($model->level1Data as $k=>$v):?>
                            <?php if($v['cid']==$model->data['level1']):?>
                                <p class="state-radio state-on" data-cid="<?= $v['cid']; ?>"></p>
                                <input type="hidden" name='level1' value="<?= $v['cid']; ?>"/>
                            <?php else:?>
                                <p class="state-radio state-off" data-cid="<?= $v['cid']; ?>"></p>
                            <?php endif;?>
                            <p class="lable-p"><?= $v['name'];?></p>
                        <?php endforeach;?>
                    </div>
                    <?php if(!empty($model->data['level2'])):?>
                    <div class="state" id="level2" style="display: block;">
                        <div class="float-l">
                            文件类型：
                        </div>
                        <?php foreach($model->level2Data as $k=>$v):?>
                            <?php if($v['cid']==$model->data['level2']):?>
                                <p class="state-radio state-on" data-cid="<?= $v['cid']; ?>"></p>
                                <input type="hidden" name='level2' value="<?= $v['cid']; ?>"/>
                            <?php else:?>
                                <p class="state-radio state-off" data-cid="<?= $v['cid']; ?>"></p>
                            <?php endif;?>
                            <p class="lable-p"><?= $v['name'];?></p>
                        <?php endforeach;?>
                    </div>
                    <?php else:?>
                        <div class="state" id="level2" style="display: none;">
                        </div>
                    <?php endif;?>
                    <?php if(!empty($model->data['level3'])):?>
                    <div class="state" id="level3" style="display: block;">
                        <div class="float-l">
                            产品分类：
                        </div>
                        <?php foreach($model->level3Data as $k=>$v):?>
                            <?php if($v['cid']==$model->data['level3']):?>
                                <p class="state-radio state-on" data-cid="<?= $v['cid']; ?>"></p>
                                <input type="hidden" name='level3' value="<?= $v['cid']; ?>"/>
                            <?php else:?>
                                <p class="state-radio state-off" data-cid="<?= $v['cid']; ?>"></p>
                            <?php endif;?>
                            <p class="lable-p"><?= $v['name'];?></p>
                        <?php endforeach;?>
                    </div>
                    <?php else:?>
                        <div class="state" id="level3" style="display: none;">
                        </div>
                    <?php endif;?>
                    <div class="input width60">
                        <label>下载链接：</label>
                        <input type="text" name="url" class="input-text400" placeholder="请输入文章标题" value="<?= $model->data['url'];?>"/>
                        <span style="color:red;">*(必填)</span>
                    </div>
                    <div class="input width60">
                        <label>下载密码：</label>
                        <input type="text" name="password" class="input-text150" placeholder="请输入文章标题" value="<?= $model->data['password'];?>"/>
                    </div>
                    <div class=" clear width1024">
                        <a href="<?= Route::Url('index');?>" class="undo">取消并返回</a>
                        <input type="submit" name="submit" id="submit" class="submit" value="确认"/>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<?php include "menu.php"; ?>
<script>
    //单选框点击样式,这里目前使用面向对象的写法，虽然简单但是过程不好理解，以后将使用foreach遍历的方法
    (function($){
        //这里使用了实践冒泡，事件代理
        $('#content-box').on('click','.state-radio', function(){
            //改变同级别元素的样式
            $(this).siblings('.state-radio').removeClass('state-on state-off');
            $(this).siblings('.state-radio').addClass('state-off');
            //改变自身的样式
            $(this).removeClass('state-off');
            $(this).addClass('state-on');
            //获取当前分类的id，并设置input的值
            var cid=$(this).data('cid');
            $(this).siblings('input').val(cid);
            //判断是否已经到达第三级目录，如果到达第三级目录就不需要请求数据
            if($(this).siblings('input').attr('name')=='level3'){
                return;
            }
            $.ajax({
                type:'post',
                url:'<?= Route::Url('edit'); ?>',
                data:{cid:cid},
                success:function(response,status,xhr){
                    //debugger;
                    var ajaxs=JSON.parse(response);
                    if(ajaxs[0]!==undefined && ajaxs[0]['level']==2){
                        var htmls=createHtml("文件类型：",ajaxs,2);
                        $('#level2').css('display','block');
                        $('#level2').html(htmls);
                        $('#level3').css('display','none');
                        $('#level3').html('');
                    }
                    if(ajaxs[0]!==undefined && ajaxs[0]['level']==3){
                        var htmls=createHtml("产品分类：",ajaxs,3);
                        $('#level3').css('display','block');
                        $('#level3').html(htmls);
                    }else{
                        $('#level3').css('display','none');
                        $('#level3').html('');
                    }

                    console.log(ajaxs);
                }
            });
            function createHtml(info,ajaxs,level){
                var htmls='';
                htmls+="<div class='float-l'>"+info+"</div>";
                htmls+="<input type='hidden' name='level"+level+"' value='' />";
                for(var i=0;i<ajaxs.length;i++){
                    htmls+="<p class='state-radio state-off' data-cid='"+ajaxs[i]['cid']+"'></p>";
                    htmls+="<p class='lable-p'>"+ajaxs[i]['name']+"</p>";
                }
                return htmls;
            }
        });
    })(jQuery)
</script>
</body>
</html>